<?php
	include_once "xAP-functions/webincludes.php";

$id=intval($_GET["id"]);
$type=intval($_GET["type"]);

$item=array();
$results=dbquery("SELECT SourceID,Location,Alias,SourceType FROM xap_pictures_source where xap_pictures_source.sourcetype=0  order by SourceType,Alias");

foreach($results as $r){
	if ($r["SourceType"]==0){
		$src[$r["SourceID"]]["lcn"]=$r["Location"];
		$src[$r["SourceID"]]["name"]=$r["Alias"];
		$src[$r["SourceID"]]["type"]=$r["SourceType"];
	}
}
$results=dbquery("SELECT distinct xap_pictures_item.SourceID,xap_pictures_item.FullFolder  FROM xap_pictures_item
inner join xap_pictures_source on xap_pictures_item.sourceid=xap_pictures_source.sourceid 
where xap_pictures_source.sourcetype=0 and xap_pictures_source.sourceid=".$id."
order by FullFolder;");
/*
$results=dbquery("SELECT SourceID,Location,Alias,SourceType FROM xap_pictures_source where sourcetype=0 order by SourceType,Alias");

foreach($results as $r){
	$item[]=array("caption"=>$r["Alias"],
					  "url"=>"iphone_picfolder.php?type=1&id=".$r["SourceID"]."#_PicturesList1");
	$title="Date";

}
*/
foreach($results as $r){

	$tmp=str_replace($src[$r["SourceID"]]["lcn"],"",$r["FullFolder"]);
	$tmp=str_replace("\\","/",$tmp);
	if($tmp[0]=="/"){$tmp=substr($tmp,1);}
	
	$item=explode("/",$tmp);
	
	//echo count($item).$tmp."<BR>";

	if($_GET["root"]=="source"){
		//if(isset($src[$r["SourceID"]]["fld"][$r["SourceID"]."-".urlencode($item[0])])==false){
			if(count($item)<2){
					$children="false";				
				}else{
					$children="true";
				}
			$src[$r["SourceID"]]["fld"][$r["SourceID"]."---".urlencode($item[0])]=array("c"=>$children,
														"name"=>$item[0]);
		//}
		}else{
			// need to fin items of this level
			$base=$_GET["root"];
			$blastitems=explode("---",$base);
			$children='false';
			//build match for item
			$lstitems=array();
			for($i=0;$i<(count($blastitems));$i++){
				$lstitems[]=$item[$i];
				$displayname=$item[$i];
			}
			$lst=implode("---",$lstitems);
			$lst=urlencode($lst);

			
			//only show items of this level
			if(substr($r["SourceID"]."---".$lst,0,strlen($base."---"))==$base."---"){
				if (isset($srcitem[$r["SourceID"]."---".$lst])==false){
				$srcitem[$r["SourceID"]."---".$lst]=array("c"=>1,
														"name"=>$displayname);	
						//echo "-".$r["FullFolder"]."         ".$lst."---<BR>";
						if(strpos("-".$r["FullFolder"],$lst."---")){
							$srcitem[$r["SourceID"]."---".$lst]["c"]++;
						}
					}else{
				$srcitem[$r["SourceID"]."---".$lst]["c"]++;
					}
			}
		}
		//print_r($src);
}	

$item=array();
if($_GET["root"]=="source"){
		$sv=$src[$id] ;
		//print_r($src);
		if(isset($sv["fld"])){
			foreach($sv["fld"] as $vkey=>$v){
				$item[]=array("caption"=>$v["name"],
					  "url"=>"iphone_pics.php?root=".$vkey."&type=".($type+1)."&id=".$id."#_PicturesList".($type+1));
		
			}
		}
	}else{
	$idraw=$_GET["root"];
	//echo $idraw;
	$arid=explode("---",$idraw);
	$sourceid=intval($arid[0]);
	unset($arid[0]);
		
		if(isset($srcitem)){
			foreach($srcitem as $skey=>$sv){		
				$item[]=array("caption"=>$sv["name"],
					  "url"=>"iphone_pics.php?root=".$skey."&type=".($type+1)."&id=".$id."#_PicturesList".($type+1));
			
			}
		}
		
		if ($sourceid>0){

			$path=implode(DIRECTORY_SEPARATOR,$arid);
			
			$sql="select Location from xAP_Pictures_Source where SourceID=".$sourceid;
			$results=dbquery($sql);
			
			$picpath=$results[0]["Location"].DIRECTORY_SEPARATOR.$path;
			$picpath=str_replace("\\'","'",$picpath);
			$picpath=str_replace("\\","\\\\",$picpath);
			//clear trailing slashes
			if(substr($picpath,-2)=="\\\\"){$picpath=substr($picpath,0,strlen($picpath)-2);}
			if(substr($picpath,-1)=="/"){$picpath=substr($picpath,0,strlen($picpath)-1);}
			
			$sql="SELECT FullLocation,SourceID,ItemID FROM xap_pictures_item where fullfolder='".dbesc($picpath)."'";
			$results=dbquery($sql);
			//echo $sql;
			foreach($results as $v){
				$img[]= "<img src='/Pictures/image.php?id=".$v["ItemID"]."&type=1' width=160 height=120>";
			}
		}
		
		
	}


$q="?";
header("Content-type: text/xml");
?>
<<?=$q?>xml version="1.0" encoding="UTF-8"?>
<root>
<title set="waPicturesList<?=$type?>"><?=$title?></title>
<part>
<destination mode="replace" zone="waPicturesList<?=$type?>"  create="true"/>
<data><![CDATA[
<? if(isset($item)): ?>
<div class="iMenu">
<UL class='iArrow'>
<? foreach($item as $k=>$v):?>
<? if($v["caption"]!=""): ?>
<LI class='iArrow'><a href='/Pictures/<?=$v["url"]?>' rev="async"><?=$v["caption"];?></a></LI>
<? endif; ?>
<? endforeach; ?>
</UL>
</div>
<? endif; ?>
<? if(isset($img)): ?>
<div class="iBlock">
<p align=center>
<? foreach($img as $v): ?>
<?=$v?><BR>
<? endforeach; ?>
</p>
</div>
<? endif; ?>
]]></data>
</part>
</root>